/*
 * Copyright (C) 2018 Logos Technology Ltd.
 */

package com.logos.common.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.fragment.app.Fragment;

import com.logos.tool.log.LogUtil;

/**
 * @author Young Ken
 */

public abstract class BaseFragment extends Fragment {

    protected BaseActivity mActivity;
    protected static final String TAG = "BaseFragment";
    protected FrameLayout mContainer;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof BaseActivity) {
            this.mActivity = (BaseActivity) context;
        }
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        LogUtil.verbose(getClass().getSimpleName(), "onViewCreated");
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        LogUtil.verbose(getClass().getSimpleName(), "onCreateView");
        mContainer = getContainer();
        return mContainer;
    }

    private FrameLayout getContainer() {
        return new FrameLayout(getActivity().getApplicationContext());
    }

    /**
     * 跳转页面
     *
     * @param cls    需要跳转的activity
     * @param bundle 参数.
     */
    public void gotoActivity(Class<? extends Activity> cls, Bundle bundle) {
        Intent intent = new Intent(getActivity(), cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    /**
     * 跳转
     *
     * @param cls cls
     */
    public void gotoActivity(Class<? extends Activity> cls) {
        gotoActivity(cls, null);
    }

    /**
     * 跳转
     *
     * @param toCls       toCls
     * @param requestCode requestCode
     */
    public void gotoActivityForResult(Class<? extends Activity> toCls, int requestCode) {
        Intent intent = new Intent(getActivity(), toCls);
        startActivityForResult(intent, requestCode);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
